package com.atguigu.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> qw = new QueryWrapper<>();

        if (params.containsKey("key")) {
            String key = params.get("key").toString();
            if (StringUtils.isNotEmpty(key)){
                qw.and(s->{s.lambda().eq(PurchaseDetailEntity::getPurchaseId,key).or().eq(PurchaseDetailEntity::getSkuId,key);});
            }
        }
        if (params.containsKey("wareId")) {
            String wareId = params.get("wareId").toString();
            if (StringUtils.isNotEmpty(wareId)){
                qw.lambda().eq(PurchaseDetailEntity::getWareId,wareId);
            }
        }
        if (params.containsKey("status")) {
            String status = params.get("status").toString();
            if (StringUtils.isNotEmpty(status)){
                qw.lambda().eq(PurchaseDetailEntity::getStatus,status);
            }
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),qw);

        return new PageUtils(page);
    }

    @Override
    public void updataPurchaseDetail(List<Long> purchaseIds) {
        LambdaQueryWrapper<PurchaseDetailEntity> qw = new LambdaQueryWrapper<>();
        for (Long purchaseId : purchaseIds) {
            qw.eq(PurchaseDetailEntity::getPurchaseId, purchaseId);
            List<PurchaseDetailEntity> list = this.list(qw);
            list.stream().forEach(s -> s.setStatus(2));
            this.updateBatchById(list);
        }
    }
}
